<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class mmaster extends CI_Model {

	function __construct() {
		parent::__construct();
        $ci = & get_instance();
        $ci->load->model('mdealer');		
	}
	
	public function randstring($idx="") {
		$idx = ($idx == '') ? 'brg' : $idx ;
		$result = uniqid($idx . "_") . "_" . md5(date("Y-m-d G:i:s"));
		return $result;
	}

	function savedatamaster($post, $type, $editstatus){
		$this->db->trans_begin();	
		switch($type){
			case 'wilayah':
				$table = "tbl_master_wilayah";
			break;
			case 'dealer':
				$table = "tbl_master_dealer";
			break;
			case 'supplier':
				$table = "tbl_master_supplier";
			break;
			case 'customer':
				$table = "tbl_master_customer";
				$post['tanggal_lahir'] = $post['tahun']."-".$post['bulan']."-".$post['tanggal'];
				unset($post['tahun']);
				unset($post['bulan']);
				unset($post['tanggal']);
			break;
			case 'gudang':
				$table = "tbl_master_gudang";
			break;
			case 'karyawan':
				$table = "tbl_master_karyawan";
				$post['tanggal_lahir'] = $post['tahun']."-".$post['bulan']."-".$post['tanggal'];
				unset($post['tahun']);
				unset($post['bulan']);
				unset($post['tanggal']);
				if(!empty($_FILES['foto_karyawan']['name'])){	
					$ext = explode('.',$_FILES['foto_karyawan']['name']);
					$exttemp = sizeof($ext) - 1;
					$extension = $ext[$exttemp];
					$upload_path = "./repository/master/foto_karyawan/";
					$base_name = md5($this->mdealer->randomString(8));
					$nama_foto =  'karyawan_'.$base_name.'.'.$extension;
					if($editstatus == 'edit'){
						$sqlPict = $this->db->query("SELECT foto_karyawan AS picture FROM ".$table." WHERE id = '".$post['id']."' ")->row();
						if($sqlPict->picture != ""){ 
							$nama_update_file = $sqlPict->picture;
							if(file_exists($upload_path.$nama_update_file)){
								unlink($upload_path.$nama_update_file);
							}
						}
					}
					
					$file = $_FILES['foto_karyawan']['name'];
					$tmp  = $_FILES['foto_karyawan']['tmp_name'];
						if(file_exists($upload_path.$nama_foto)){
							unlink($upload_path.$nama_foto);
							$uploadfile = $upload_path.$nama_foto;
						}	
						else{
							$uploadfile = $upload_path.$nama_foto;
						}
					move_uploaded_file($tmp, $uploadfile);
						if (!chmod($uploadfile, 0775)) {
							 echo "Gagal mengupload file";
							 exit;
						}
					
					$post['foto_karyawan'] = $nama_foto;
				}
			break;
			case 'pihak':
				$table = "tbl_master_pihak_ketiga";
				$post['tanggal_lahir'] = $post['tahun']."-".$post['bulan']."-".$post['tanggal'];
				unset($post['tahun']);
				unset($post['bulan']);
				unset($post['tanggal']);
			break;
			case 'kas':
				$table = "tbl_master_kas";
			break;
			case 'aktiva':
				$table = "tbl_master_aktiva";
			break;
			case 'warna':
				$table = "tbl_master_warna";
			break;
			case 'motor':
				$table = "tbl_master_motor";
				
				$post['nominal_harga_beli'] = str_replace(",","",$post['nominal_harga_beli']);
				$post['ppn_harga_beli'] = str_replace(",","",$post['ppn_harga_beli']);
				
				//============ proses harga jual begin ==============//
				if ($editstatus != 'edit') {
					$post['id'] = $this->randstring();	
				}

				$post2  = array();
				$table2 = "tbl_master_harga_jual";

				foreach($post['sts_detil'] as $k=>$v) {
					$post2['id_barang'] = $post['id'];
					$post2['nom_hrg_jual'] = str_replace(",","",$post['nom_hrg_jual'][$k]);
					$post2['ppn_hrg_jual'] = str_replace(",","",$post['ppn_hrg_jual'][$k]);
					$post2['kondisi_ppn_hrg_jual'] = $post['kondisi_ppn_hrg_jual'][$k];
					$post2['tingkat_hrg_jual'] = $post['tingkat_hrg_jual'][$k];
					
					if ($editstatus == 'edit') {
						if ($post['id_detil'][$k] != "") {
							$where = $post['id_detil'][$k];
							$this->db->update($table2, $post2, array('id'=>$where));
						} else {
							if ($post2['tingkat_hrg_jual'] != "") {
								$this->db->insert($table2, $post2);	
							}
						}	
					} else {
						$this->db->insert($table2, $post2);
					}
				}

				unset($post['kondisi_ppn_hrg_jual']);
				unset($post['nom_hrg_jual']);
				unset($post['ppn_hrg_jual']);
				unset($post['tingkat_hrg_jual']);
				unset($post['id_detil']);
				unset($post['sts_detil']);
				//============ proses harga jual end ==============//
			break;
			case 'service':
				$table = "tbl_master_service";
			break;
			case 'generator':
			case 'sparepart':
			case 'peralatan_service':
			case 'barang_promosi':
				$this->load->helper('main');
				if ($editstatus == "add") { $post['kode_gambar'] = randcode(); }
				
				$table 				 = "tbl_master_barang";
				$tbl_image 			 = "tbl_master_gambar_barang";
				$count 				 = 0;
				
				
				//============ proses harga jual begin ==============//
				if ($editstatus != 'edit') {
					$post['id'] = $this->randstring();	
				}

				$post2  = array();
				$table2 = "tbl_master_harga_jual";

				foreach($post['sts_detil'] as $k=>$v) {
					$post2['id_barang'] = $post['id'];
					$post2['nom_hrg_jual'] = str_replace(",","",$post['nom_hrg_jual'][$k]);
					$post2['ppn_hrg_jual'] = str_replace(",","",$post['ppn_hrg_jual'][$k]);
					$post2['kondisi_ppn_hrg_jual'] = $post['kondisi_ppn_hrg_jual'][$k];
					$post2['tingkat_hrg_jual'] = $post['tingkat_hrg_jual'][$k];
					
					if ($editstatus == 'edit') {
						if ($post['id_detil'][$k] != "") {
							$where = $post['id_detil'][$k];
							$this->db->update($table2, $post2, array('id'=>$where));
						} else {
							if ($post2['tingkat_hrg_jual'] != "") {
								$this->db->insert($table2, $post2);	
							}
						}	
					} else {
						if ($post2['tingkat_hrg_jual'] != "") {
							$this->db->insert($table2, $post2);
						}
					}
				}

				unset($post['kondisi_ppn_hrg_jual']);
				unset($post['nom_hrg_jual']);
				unset($post['ppn_hrg_jual']);
				unset($post['tingkat_hrg_jual']);
				unset($post['id_detil']);
				unset($post['sts_detil']);
				//============ proses harga jual end ==============//


				foreach($_FILES['foto_barang']['name'] as $filename) {
					if(!empty($filename)) {
						$ext = explode('.', $filename);
						$exttemp = sizeof($ext) - 1;
						$extension = $ext[$exttemp];
						$upload_path = "./repository/master/foto_barang/";
						$base_name = md5($this->mdealer->randomString(8));
						$nama_foto =  'barang_'.$base_name.'.'.$extension;
						
						if($editstatus == 'edit' && !empty($post['id_foto_barang'][$count]) ){
							$sqlPict = $this->db->query("SELECT file_name AS picture FROM tbl_master_gambar_barang 
														WHERE kode_gambar = '".$post['kode_gambar']."' 
														AND id = '".$post['id_foto_barang'][$count]."' ")->row();
							if($sqlPict->picture != ""){ 
								$nama_update_file = $sqlPict->picture;
								if(file_exists($upload_path.$nama_update_file)){
									unlink($upload_path.$nama_update_file);
								}
							}
						}
						
						$file = $filename;
						$tmp  = $_FILES['foto_barang']['tmp_name'][$count];
							if(file_exists($upload_path.$nama_foto)) {
								unlink($upload_path.$nama_foto);
								$uploadfile = $upload_path.$nama_foto;
							} else {
								$uploadfile = $upload_path.$nama_foto;
							}
						move_uploaded_file($tmp, $uploadfile);
							if (!chmod($uploadfile, 0775)) {
								 echo "Gagal mengupload file";
								 exit;
							}
						
						$img_array = array(
									'file_name'=> $nama_foto,
									'kode_gambar'=> $post['kode_gambar'],
									'keterangan_gambar'=> $post['keterangan_gambar'][$count]
								);
						
						if (isset($post['id_foto_barang'][$count]) == FALSE) {
							$id_foto = "";
						} else {
							$id_foto = $post['id_foto_barang'][$count];
						}
						
						$this->saveimage($tbl_image, $img_array , $editstatus, $id_foto);
					} else {						
						if($editstatus == 'edit' && $post['id_foto_barang'][$count] != "") {
							$id_foto = $post['id_foto_barang'][$count];
							$img_array = array(
									'kode_gambar'=> $post['kode_gambar'],
									'keterangan_gambar'=> $post['keterangan_gambar'][$count]
								);
							$this->saveimage($tbl_image, $img_array , 'edit', $id_foto);
						}
					}
					$count++;
				}
				
				$post['nominal_harga_beli'] = str_replace(",","",$post['nominal_harga_beli']);
				$post['ppn_harga_beli'] = str_replace(",","",$post['ppn_harga_beli']);

				/*$post['nominal_harga_jual'] = str_replace(",","",$post['nominal_harga_jual']);
				$post['ppn_harga_jual'] = str_replace(",","",$post['ppn_harga_jual']);*/

				unset($post['dpp_harga_beli']);
				//unset($post['dpp_harga_jual']);
				unset($post['nominal_ppn_harga_beli']);
				//unset($post['nominal_ppn_harga_jual']);
				unset($post['harga_beli_plus_ppn']);
				//unset($post['harga_jual_plus_ppn']);
				unset($post['id_foto_barang']);
				unset($post['keterangan_gambar']);
				if ($editstatus == "edit") { unset($post['kode_gambar']); }
			break;

			// master proses
			case "proses_promosi_unit":
			case "proses_promosi_service":
			case "proses_promosi_sparepart":
				$table = "tbl_master_program_promosi";
				
				if ($editstatus != 'edit') {
					$post['id'] = $this->randstring('promo');	
				}
				
				if ($type == "proses_promosi_unit") {
					// managing promotion dealer
					if($post['promosi_all_dealer'] == 'N') {
						$postd  = array();
						$tabled = "tbl_master_promosi_dealer";

						foreach($post['sts_detil'] as $k=>$v) {
							$postd['id_promosi'] 			= $post['id'];
							$postd['group_promosi_dealer'] 	= $post['group_promosi_dealer'][$k];
							$postd['promosi_dealer'] 		= $post['promosi_dealer'][$k];

							
							if ($editstatus == 'edit') {
								if ($post['id_detil'][$k] != "") {
									$where = $post['id_detil'][$k];
									$this->db->update($tabled, $postd, array('id'=>$where));
								} else {
									if ($postd['group_promosi_dealer'] != "") {
										$this->db->insert($tabled, $postd);	
									}
								}	
							} else {
								if ($postd['group_promosi_dealer'] != "") {
									$this->db->insert($tabled, $postd);
								}
							}
						}
					}

					//managing gift data support
					if ($post['jenis_penjualan'] != "") {
						$posts = array();
						$tables = "tbl_master_promosi_hadiah";

						foreach($post['sts_detil2'] as $k=>$v) {
							$posts['id_promosi']   = $post['id'];
							$posts['brg_promosi']  = $post['brg_promosi'][$k];
							$posts['brg_hadiah_a'] = $post['brg_hadiah_a'][$k];
							$posts['qty_hadiah_a'] = $post['qty_hadiah_a'][$k];
							$posts['brg_hadiah_b'] = $post['brg_hadiah_b'][$k];
							$posts['qty_hadiah_b'] = $post['qty_hadiah_b'][$k];
							$posts['brg_hadiah_c'] = $post['brg_hadiah_c'][$k];
							$posts['qty_hadiah_c'] = $post['qty_hadiah_c'][$k];

							
							if ($editstatus == 'edit') {
								if ($post['id_detil2'][$k] != "") {
									$where = $post['id_detil2'][$k];
									$this->db->update($tables, $posts, array('id'=>$where));
								} else {
									if ($posts['brg_promosi'] != "") {
										$this->db->insert($tables, $posts);	
									}
								}	
							} else {
								if ($posts['brg_promosi'] != "") {
									$this->db->insert($tables, $posts);
								}
							}
						}
					}

					// managing dealer credit
					if ($post['jenis_penjualan'] == "KD" || $post['jenis_penjualan'] == "T") {
						$postkd = array();
						$tablekd = "tbl_master_promosi_kredit_dealer";

						foreach($post['sts_detil3'] as $k=>$v) {
							$postkd['id_promosi']	  = $post['id'];
							$postkd['nama_barang_KD'] = $post['nama_barang_KD'][$k];
							$postkd['nilai_discount'] = $post['nilai_discount'][$k];

							if ($editstatus == 'edit') {
								if ($post['id_detil3'][$k] != "") {
									$where = $post['id_detil3'][$k];
									$this->db->update($tablekd, $postkd, array('id'=>$where));
								} else {
									if ($postkd['nama_barang_KD'] != "") {
										$this->db->insert($tablekd, $postkd);	
									}
								}	
							} else {
								if ($postkd['nama_barang_KD'] != "") {
									$this->db->insert($tablekd, $postkd);
								}
							}
						}
					}

					// managing leasing credit
					if ($post['jenis_penjualan'] == "KL") {
						$postkl = array();
						$tablekl = "tbl_master_promosi_kredit_leasing";

						foreach($post['sts_detil4'] as $k=>$v) {
							$postkl['id_promosi']	  	 = $post['id'];
							$postkl['nama_barang_KL'] 	 = $post['nama_barang_KL'][$k];
							$postkl['kondisi_uang_muka'] = $post['kondisi_uang_muka'][$k];
							$postkl['tenor_a'] 			 = $post['tenor_a'][$k];
							$postkl['nilai_subsidi_a']   = str_replace(",","",$post['nilai_subsidi_a'][$k]);
							$postkl['tenor_b'] 			 = $post['tenor_b'][$k];
							$postkl['nilai_subsidi_b']   = str_replace(",","",$post['nilai_subsidi_b'][$k]);
							$postkl['tenor_c'] 			 = $post['tenor_c'][$k];
							$postkl['nilai_subsidi_c']   = str_replace(",","",$post['nilai_subsidi_c'][$k]);
							$postkl['tenor_d'] 			 = $post['tenor_d'][$k];
							$postkl['nilai_subsidi_d']   = str_replace(",","",$post['nilai_subsidi_d'][$k]);
							$postkl['tenor_e'] 			 = $post['tenor_e'][$k];
							$postkl['nilai_subsidi_e']   = str_replace(",","",$post['nilai_subsidi_e'][$k]);
							$postkl['tenor_f'] 			 = $post['tenor_f'][$k];
							$postkl['nilai_subsidi_f']   = str_replace(",","",$post['nilai_subsidi_f'][$k]);

							if ($editstatus == 'edit') {
								if ($post['id_detil4'][$k] != "") {
									$where = $post['id_detil4'][$k];
									$this->db->update($tablekl, $postkl, array('id'=>$where));
								} else {
									if ($postkl['nama_barang_KL'] != "") {
										$this->db->insert($tablekl, $postkl);	
									}
								}	
							} else {
								if ($postkl['nama_barang_KL'] != "") {
									$this->db->insert($tablekl, $postkl);
								}
							}
						}
					}
				}
				
				if ($type == "proses_promosi_service") {
					// managing promotion dealer
					$posth  = array();
					$tableh = "tbl_master_hadiah_langsung";

					foreach($post['sts_detil'] as $k=>$v) {
						$posth['id_promosi'] 		 	 = $post['id'];
						$posth['kategori_barang_hadiah'] = $post['kategori_barang_hadiah'][$k];
						$posth['nama_barang_hadiah'] 	 = $post['nama_barang_hadiah'][$k];
						$posth['qty_barang'] 		 	 = $post['qty_barang'][$k];
						$posth['satuan'] 			 	 = $post['satuan'][$k];

						
						if ($editstatus == 'edit') {
							if ($post['id_detil'][$k] != "") {
								$where = $post['id_detil'][$k];
								$this->db->update($tableh, $posth, array('id'=>$where));
							} else {
								if ($posth['nama_barang_hadiah'] != "") {
									$this->db->insert($tableh, $posth);	
								}
							}	
						} else {
							if ($posth['nama_barang_hadiah'] != "") {
								$this->db->insert($tableh, $posth);
							}
						}
					}
				}

				if ($type == "proses_promosi_sparepart") {
					$posth  = array();
					$tableh = "tbl_master_penjualan_paket";

					foreach($post['sts_detil'] as $k=>$v) {
						$posth['id_promosi'] 		 = $post['id'];
						$posth['kode_barang_paket']  = $post['kode_barang_paket'][$k];
						$posth['nama_barang_paket']  = $post['nama_barang_paket'][$k];
						$posth['harga_jual'] 		 = str_replace(",","",$post['harga_jual'][$k]);
						$posth['quantity'] 		 	 = $post['quantity'][$k];
						$posth['satuan_barang'] 	 = $post['satuan_barang'][$k];

						if ($editstatus == 'edit') {
							if ($post['id_detil'][$k] != "") {
								$where = $post['id_detil'][$k];
								$this->db->update($tableh, $posth, array('id'=>$where));
							} else {
								if ($posth['nama_barang_paket'] != "") {
									$this->db->insert($tableh, $posth);	
								}
							}	
						} else {
							if ($posth['nama_barang_paket'] != "") {
								$this->db->insert($tableh, $posth);
							}
						}
					}
				}

				unset($post['kode_barang_paket']);
				unset($post['nama_barang_paket']);
				unset($post['harga_jual']);
				unset($post['quantity']);
				unset($post['satuan_barang']);
 
				unset($post['kategori_barang_hadiah']);
				unset($post['nama_barang_hadiah']);
				unset($post['qty_barang']);
				unset($post['satuan']);

				unset($post['brg_promosi']);
				unset($post['brg_hadiah_a']);
				unset($post['qty_hadiah_a']);
				unset($post['brg_hadiah_b']);
				unset($post['qty_hadiah_b']);
				unset($post['brg_hadiah_c']);
				unset($post['qty_hadiah_c']);

				unset($post['group_promosi_dealer']);
				unset($post['promosi_dealer']);
				
				unset($post['nama_barang_KD']);
				unset($post['nilai_discount']);

				unset($post['nama_barang_KL']);
				unset($post['kondisi_uang_muka']);
				unset($post['tenor_a']);
				unset($post['nilai_subsidi_a']);
				unset($post['tenor_b']);
				unset($post['nilai_subsidi_b']);
				unset($post['tenor_c']);
				unset($post['nilai_subsidi_c']);
				unset($post['tenor_d']);
				unset($post['nilai_subsidi_d']);
				unset($post['tenor_e']);
				unset($post['nilai_subsidi_e']);
				unset($post['tenor_f']);
				unset($post['nilai_subsidi_f']);
				
				unset($post['id_detil']);
				unset($post['sts_detil']);
				unset($post['id_detil2']);
				unset($post['sts_detil2']);
				unset($post['id_detil3']);
				unset($post['sts_detil3']);
				unset($post['id_detil4']);
				unset($post['sts_detil4']);
			break;

			case "proses_bbn_unit":
			break;

			case "proses_pembiayaan_unit":
				$table = "tbl_master_proses_pembiayaan";
				
				if ($editstatus != 'edit') {
					$post['id'] = $this->randstring('promo');	
				}

				// jenis support subsidi
				if ($post['id_jenis_support'] == "S") {
					// support subsidi
					$post_s = array();
					$table_s = "tbl_master_jenis_support";

					foreach($post['sts_detil'] as $k=>$v) {
						$post_s['id_proses_pembiayaan'] = $post['id'];
						$post_s['jenis_support'] 		= $post['id_jenis_support'];
						$post_s['nama_barang'] 			= $post['subsidi_nm_brg'][$k];
						$post_s['tgl_mulai_berlaku'] 	= $post['subsidi_tgl_mulai'][$k];
						$post_s['tgl_akhir_berlaku'] 	= $post['subsidi_tgl_akhir'][$k];
						$post_s['nilai'] 				= str_replace(",","",$post['subsidi_nilai'][$k]);

						if ($editstatus == 'edit') {
							if ($post['id_detil'][$k] != "") {
								$where = $post['id_detil'][$k];
								$this->db->update($table_s, $post_s, array('id'=>$where));
							} else {
								if ($post_s['nama_barang'] != "") {
									$this->db->insert($table_s, $post_s);	
								}
							}	
						} else {
							if ($post_s['nama_barang'] != "") {
								$this->db->insert($table_s, $post_s);
							}
						}
					}

					// support subsidi additional
					$post_sa = array();
					$table_sa = "tbl_master_subsidi_add";

					foreach($post['sts_detil2'] as $k=>$v) {
						$post_sa['id_proses_pembiayaan'] = $post['id'];
						$post_sa['nama_barang']			 = $post['sub_add_nm_brg'][$k];
						$post_sa['min_uang_muka'] 		 = str_replace(",","",$post['sub_add_min_uang_muka'][$k]);
						$post_sa['tgl_mulai_berlaku'] 	 = $post['sub_add_tgl_mulai'][$k];
						$post_sa['tgl_berakhir'] 		 = $post['sub_add_tgl_akhir'][$k];
						$post_sa['tenor_a'] 			 = $post['sub_add_tenor_a'][$k];
						$post_sa['nilai_subsidi_a']		 = str_replace(",","",$post['sub_add_nilai_a'][$k]);
						$post_sa['tenor_b'] 			 = $post['sub_add_tenor_b'][$k];
						$post_sa['nilai_subsidi_b']   	 = str_replace(",","",$post['sub_add_nilai_b'][$k]);
						$post_sa['tenor_c'] 			 = $post['sub_add_tenor_c'][$k];
						$post_sa['nilai_subsidi_c']   	 = str_replace(",","",$post['sub_add_nilai_c'][$k]);
						$post_sa['tenor_d'] 			 = $post['sub_add_tenor_d'][$k];
						$post_sa['nilai_subsidi_d']   	 = str_replace(",","",$post['sub_add_nilai_d'][$k]);
						$post_sa['tenor_e'] 			 = $post['sub_add_tenor_e'][$k];
						$post_sa['nilai_subsidi_e']   	 = str_replace(",","",$post['sub_add_nilai_e'][$k]);
						//$post_sa['tenor_f'] 			 = $post['sub_add_tenor_f'][$k];
						//$post_sa['nilai_subsidi_f']   	 = $post['sub_add_nilai_f'][$k];

						if ($editstatus == 'edit') {
							if ($post['id_detil2'][$k] != "") {
								$where = $post['id_detil2'][$k];
								$this->db->update($table_sa, $post_sa, array('id'=>$where));
							} else {
								if ($post_sa['nama_barang'] != "") {
									$this->db->insert($table_sa, $post_sa);	
								}
							}	
						} else {
							if ($post_sa['nama_barang'] != "") {
								$this->db->insert($table_sa, $post_sa);
							}
						}
					}
				}

				// jenis support Incentive
				if ($post['id_jenis_support'] == "I") {
					// support subsidi
					$post_s = array();
					$table_s = "tbl_master_jenis_support";

					foreach($post['sts_detil3'] as $k=>$v) {
						$post_s['id_proses_pembiayaan'] = $post['id'];
						$post_s['jenis_support'] 		= $post['id_jenis_support'];
						$post_s['nama_barang'] 			= $post['incentive_nm_brg'][$k];
						$post_s['tgl_mulai_berlaku'] 	= $post['incentive_tgl_mulai'][$k];
						$post_s['tgl_akhir_berlaku'] 	= $post['incentive_tgl_akhir'][$k];
						$post_s['nilai'] 				= str_replace(",","",$post['incentive_nilai'][$k]);

						if ($editstatus == 'edit') {
							if ($post['id_detil3'][$k] != "") {
								$where = $post['id_detil3'][$k];
								$this->db->update($table_s, $post_s, array('id'=>$where));
							} else {
								if ($post_s['nama_barang'] != "") {
									$this->db->insert($table_s, $post_s);	
								}
							}	
						} else {
							if ($post_s['nama_barang'] != "") {
								$this->db->insert($table_s, $post_s);
							}
						}
					}
				}

				// jenis support promo
				if ($post['id_jenis_support'] == "P") {
					// support subsidi
					$post_s = array();
					$table_s = "tbl_master_jenis_support";

					foreach($post['sts_detil4'] as $k=>$v) {
						$post_s['id_proses_pembiayaan'] = $post['id'];
						$post_s['jenis_support'] 		= $post['id_jenis_support'];
						$post_s['nama_barang'] 			= $post['promo_nm_brg'][$k];
						$post_s['tgl_mulai_berlaku'] 	= $post['promo_tgl_mulai'][$k];
						$post_s['tgl_akhir_berlaku'] 	= $post['promo_tgl_akhir'][$k];
						$post_s['nilai'] 				= str_replace(",","",$post['promo_nilai'][$k]);

						if ($editstatus == 'edit') {
							if ($post['id_detil4'][$k] != "") {
								$where = $post['id_detil4'][$k];
								$this->db->update($table_s, $post_s, array('id'=>$where));
							} else {
								if ($post_s['nama_barang'] != "") {
									$this->db->insert($table_s, $post_s);	
								}
							}	
						} else {
							if ($post_s['nama_barang'] != "") {
								$this->db->insert($table_s, $post_s);
							}
						}
					}
				}

				unset($post['subsidi_nm_brg']);
				unset($post['subsidi_tgl_mulai']);
				unset($post['subsidi_tgl_akhir']);
				unset($post['subsidi_nilai']);
				unset($post['incentive_nm_brg']);
				unset($post['incentive_tgl_mulai']);
				unset($post['incentive_tgl_akhir']);
				unset($post['incentive_nilai']);
				unset($post['promo_nm_brg']);
				unset($post['promo_tgl_mulai']);
				unset($post['promo_tgl_akhir']);
				unset($post['promo_nilai']);

				unset($post['sub_add_nm_brg']);
				unset($post['sub_add_min_uang_muka']);
				unset($post['sub_add_tgl_mulai']);
				unset($post['sub_add_tgl_akhir']);
				unset($post['sub_add_tenor_a']);
				unset($post['sub_add_nilai_a']);
				unset($post['sub_add_tenor_b']);
				unset($post['sub_add_nilai_b']);
				unset($post['sub_add_tenor_c']);
				unset($post['sub_add_nilai_c']);
				unset($post['sub_add_tenor_d']);
				unset($post['sub_add_nilai_d']);
				unset($post['sub_add_tenor_e']);
				unset($post['sub_add_nilai_e']);
				unset($post['sub_add_tenor_f']);
				unset($post['sub_add_nilai_f']);

				unset($post['sts_detil']);
				unset($post['id_detil']);
				unset($post['sts_detil2']);
				unset($post['id_detil2']);
				unset($post['sts_detil3']);
				unset($post['id_detil3']);
				unset($post['sts_detil4']);
				unset($post['id_detil4']);
			break;

			case "proses_service_ksg":
			break;

			// master akutansi
			case "kode_akun":
				$table = "tbl_master_kode_akun";
			break;

			case "akun_kas":
				$table = "tbl_master_akun_kas";
			break;

			case "akun_barang_jasa":
				$table = "tbl_master_akun_barang_jasa";
			break;

			case "akun_aktifa_tetap":
				$table = "tbl_master_akun_aktiva_tetap";
			break;

			case "akun_saldo":
				$table = "tbl_master_akun_saldo";
			break;
		}
		
		if($editstatus == 'add') {
			$this->db->insert($table, $post);
		}elseif($editstatus == 'edit'){
			$this->db->update($table, $post, array('id'=>$post['id']));
		}
		
		if($this->db->trans_status() == false){
			$this->db->trans_rollback();
			return "Data not saved";
		}else{
			return $this->db->trans_commit();
		}
	}
	
	function saveimage($table="", $post="", $type="", $id="") {
		if($type == 'add'){
			$result = $this->db->insert($table, $post);
		}elseif($type == 'edit') {
			if(!empty($id)) {
				$result = $this->db->update($table, $post, array('id'=>$id));
			} else {
				$result = $this->db->insert($table, $post);
			}
		}
		return $result;
	}
	
	function getdata($param=""){
		$select = "";
		$from	= "";
		$where	= "WHERE 1=1 ";
		$join	= "";
		
		switch($param){
			case "wilayah":
				$select .= "A.*, C.nama_provinsi, D.nama_kabupaten, E.nama_kecamatan, F.nama_kelurahan ";
				$from 	.= "tbl_master_wilayah";
				$join	.= "LEFT JOIN idx_wilayah_provinsi C ON A.id_provinsi = C.id ";
				$join	.= "LEFT JOIN idx_wilayah_kabupaten D ON A.id_kabupaten = D.id ";
				$join	.= "LEFT JOIN idx_wilayah_kecamatan E ON A.id_kecamatan = E.id ";
				$join	.= "LEFT JOIN idx_wilayah_kelurahan F ON A.id_kelurahan = F.id ";

			break;
			case "dealer":
				$select .= "A.*, C.nama_provinsi, D.nama_kabupaten, E.nama_kecamatan, F.nama_kelurahan, G.nama_group_dealer";
				$from 	.= "tbl_master_dealer";
				$join	.= "LEFT JOIN tbl_master_wilayah B ON A.id_wilayah = B.id ";
				$join	.= "LEFT JOIN idx_wilayah_provinsi C ON B.id_provinsi = C.id ";
				$join	.= "LEFT JOIN idx_wilayah_kabupaten D ON B.id_kabupaten = D.id ";
				$join	.= "LEFT JOIN idx_wilayah_kecamatan E ON B.id_kecamatan = E.id ";
				$join	.= "LEFT JOIN idx_wilayah_kelurahan F ON B.id_kelurahan = F.id ";
				$join	.= "LEFT JOIN idx_group_dealer G ON A.id_group_dealer = G.id ";
			break;
			case "supplier":
				$select .= "A.*, C.nama_provinsi, D.nama_kabupaten, E.nama_kecamatan, F.nama_kelurahan, G.nama_group_supplier";
				$from 	.= "tbl_master_supplier";
				$join	.= "LEFT JOIN tbl_master_wilayah B ON A.id_wilayah = B.id ";
				$join	.= "LEFT JOIN idx_wilayah_provinsi C ON B.id_provinsi = C.id ";
				$join	.= "LEFT JOIN idx_wilayah_kabupaten D ON B.id_kabupaten = D.id ";
				$join	.= "LEFT JOIN idx_wilayah_kecamatan E ON B.id_kecamatan = E.id ";
				$join	.= "LEFT JOIN idx_wilayah_kelurahan F ON B.id_kelurahan = F.id ";
				$join	.= "LEFT JOIN idx_group_supplier G ON A.id_group_supplier = G.id ";
			break;
			case "customer":
				$select .= "A.*, B.nama_group_customer, C.nama_agama, D.status_tmp_tinggal, E.nama_jenis_pekerjaan, DATE_FORMAT(A.tanggal_lahir,'%d-%M-%Y') as tgl_lahir";
				$from 	.= "tbl_master_customer";
				$join	.= "LEFT JOIN idx_group_customer B ON A.id_group_customer = B.id ";
				$join	.= "LEFT JOIN idx_agama C ON A.id_agama = C.id ";
				$join	.= "LEFT JOIN idx_status_tempat_tinggal D ON A.id_status_tmp_tinggal_customer = D.id ";
				$join	.= "LEFT JOIN idx_jenis_pekerjaan E ON A.id_pekerjaan = E.id ";
			break;
			case "gudang":
				$select .= "A.*, B.nama_group_dealer, C.nama_dealer, D.nama_divisi";
				$from 	.= "tbl_master_gudang";
				$join	.= "LEFT JOIN idx_group_dealer B ON A.id_group_dealer = B.id ";
				$join	.= "LEFT JOIN tbl_master_dealer C ON A.id_dealer = C.id ";
				$join	.= "LEFT JOIN idx_divisi D ON A.id_divisi = D.id ";
			break;
			case "karyawan":
				$select .= "A.*, B.nama_dealer, C.nama_divisi, D.nama_departemen, E.nama_agama, F.status_karyawan, G.nama_group_dealer, DATE_FORMAT(A.tanggal_lahir,'%d-%M-%Y') as tgl_lahir, DATE_FORMAT(A.tanggal_masuk_kerja,'%d-%M-%Y') as tgl_msk_kerja ";
				$from 	.= "tbl_master_karyawan";
				$join	.= "LEFT JOIN tbl_master_dealer B ON A.id_dealer = B.id ";
				$join	.= "LEFT JOIN idx_divisi C ON A.id_divisi = C.id ";
				$join	.= "LEFT JOIN idx_departemen D ON A.id_departemen = D.id ";
				$join	.= "LEFT JOIN idx_agama E ON A.id_agama = E.id ";
				$join	.= "LEFT JOIN idx_status_karyawan F ON A.id_status_karyawan = F.id ";
				$join	.= "LEFT JOIN idx_group_dealer G ON A.id_group_dealer = G.id ";
			break;
			case "pihak":
				$select .= "A.*, B.nama_agama, C.status_tmp_tinggal, DATE_FORMAT(A.tanggal_lahir,'%d-%M-%Y') as tgl_lahir ";
				$from 	.= "tbl_master_pihak_ketiga";
				$join	.= "LEFT JOIN idx_agama B ON A.id_agama = B.id ";
				$join	.= "LEFT JOIN idx_status_tempat_tinggal C ON A.id_status_tempat_tinggal = C.id ";
			break;
			case "kas":
				$select .= "A.*, B.nama_group_dealer, C.nama_dealer";
				$from 	.= "tbl_master_kas";
				$join	.= "LEFT JOIN idx_group_dealer B ON A.id_group_dealer = B.id ";
				$join	.= "LEFT JOIN tbl_master_dealer C ON A.id_dealer = C.id ";
			break;
			case "aktiva":
				$select .= "A.*, B.nama_group_dealer, C.nama_dealer, DATE_FORMAT(A.tanggal_pembelian,'%d-%M-%Y') as tgl_beli, DATE_FORMAT(A.tanggal_pemakaian,'%d-%M-%Y') as tgl_pakai";
				$from 	.= "tbl_master_aktiva";
				$join	.= "LEFT JOIN idx_group_dealer B ON A.id_group_dealer = B.id ";
				$join	.= "LEFT JOIN tbl_master_dealer C ON A.id_dealer = C.id ";
			break;
			case "warna":
				$select .= "A.* ";
				$from 	.= "tbl_master_warna";
			break;

			//Master Barang & Jasa;
			case "motor":
				$select .= "A.*, B.nama_group_barang, C.kategori_barang";
				$from 	.= "tbl_master_motor";
				$join	.= "LEFT JOIN idx_group_barang B ON A.id_group_barang = B.id ";
				$join	.= "LEFT JOIN idx_kategori_barang C ON A.id_kategori_barang = C.id ";
			break;
			case "sparepart":
			case "generator":
			case "peralatan_service":
			case "barang_promosi":
				if ($param == "sparepart") { $i = '0'; }
				if ($param == "generator") { $i = '1'; }
				if ($param == "peralatan_service") { $i = '2'; }
				if ($param == "barang_promosi") { $i = '3'; }
				
				$select .= "A.*, B.nama_group_barang, C.kategori_barang";
				$from 	.= "tbl_master_barang";
				$join	.= "LEFT JOIN idx_group_barang B ON A.id_group_barang = B.id ";
				$join	.= "LEFT JOIN idx_kategori_barang C ON A.id_kategori_barang = C.id ";
				$where	.= "AND A.jenis_barang = '".$i."' ";
			break;
			case "service":
				$select .= "A.*, B.nama_group_service, C.nama_kategori_service";
				$from 	.= "tbl_master_service";
				$join	.= "LEFT JOIN idx_group_service B ON A.id_group_service = B.id ";
				$join	.= "LEFT JOIN idx_kategori_service C ON A.id_kategori_service = C.id ";
			break;
			
			case "barang_promosi":
				$select .= "A.*, B.nama_group_barang, C.kategori_barang";
				$from 	.= "tbl_master_barangpromosi";
				$join	.= "LEFT JOIN idx_group_barang B ON A.kode_group_barang = B.kode_group_barang ";
				$join	.= "LEFT JOIN idx_kategori_barang C ON A.kode_kategori_barang = C.kode_kategori_barang ";
			break;
			
			// master proses
			case "proses_promosi_unit":
			case "proses_promosi_service":
			case "proses_promosi_sparepart":
				if ($param == "proses_promosi_unit") { $i = 'pr_unit'; }
				if ($param == "proses_promosi_service") { $i = 'pr_service'; }
				if ($param == "proses_promosi_sparepart") { $i = 'pr_sparepart'; }
				
				$select .= "A.* ";
				$from 	.= "tbl_master_program_promosi";
				$where	.= "AND A.jenis_promosi = '".$i."' ";
				if ($param == "proses_promosi_service") {
					$select .= " , B.nama_group_dealer, C.nama_dealer ";
					$join	.= "LEFT JOIN idx_group_dealer B ON A.id_group_dealer = B.id ";
					$join	.= "LEFT JOIN tbl_master_dealer C ON A.id_dealer = C.id ";
				}
			break;

			case "proses_bbn_unit":
				$select .= "A.*, B.kategori_barang ";
				$from 	.= "tbl_master_proses_bbn ";
				$join	.= "LEFT JOIN idx_kategori_barang B ON A.kode_kategori_barang = B.id ";
			break;

			case "proses_pembiayaan_unit":
				$select .= "A.*, B.nama_group_supplier, C.nama_supplier ";
				$from 	.= "tbl_master_proses_pembiayaan ";
				$join	.= "LEFT JOIN idx_group_supplier B ON A.id_group_supplier = B.id ";
				$join	.= "LEFT JOIN tbl_master_supplier C ON A.id_supplier = C.id ";
			break;

			case "proses_service_ksg":
				$select .= "A.*, B.nama_supplier, C.kategori_barang ";
				$from 	.= "tbl_master_proses_ksg ";
				$join	.= "LEFT JOIN tbl_master_supplier B ON A.id_supplier = B.id ";
				$join	.= "LEFT JOIN idx_kategori_barang C ON A.id_kategori_barang = B.id ";

			break;

			// master akutansi
			case "kode_akun":
				$select .= "A.*";
				$from .= "tbl_master_kode_akun";
			break;

			case "akun_kas":
				$select .= "A.*, B.nama_group_dealer, C.nama_dealer, D.nama_kas, E.nama_akun AS 'type_akun', F.nama_akun AS 'bagian_dari_akun', G.nama_akun AS 'bagian_dari_sub_akun' ";
				$from 	.= "tbl_master_akun_kas";
				$join	.= "LEFT JOIN idx_group_dealer B ON A.id_group_dealer = B.id ";
				$join	.= "LEFT JOIN tbl_master_dealer C ON A.id_dealer = C.id ";
				$join	.= "LEFT JOIN tbl_master_kas D ON A.id_kas = D.id ";
				$join	.= "LEFT JOIN tbl_master_kode_akun E ON A.type_akun = E.kode_akun ";
				$join	.= "LEFT JOIN tbl_master_kode_akun F ON A.bagian_dari_akun = F.kode_akun ";
				$join	.= "LEFT JOIN tbl_master_kode_akun G ON A.bagian_dari_sub_akun = G.kode_akun ";
			break;

			case "akun_barang_jasa":
				$select .= "A.*, B.nama_group_barang,C.kategori_barang, D.nama_type_barang, 
							E.nama_akun AS akun_persediaan";
				$from .= "tbl_master_akun_barang_jasa ";
				$join .= "LEFT JOIN idx_group_barang B ON A.id_group_barang=B.id ";
				$join .= "LEFT JOIN idx_kategori_barang C ON A.id_kategori_barang=C.id ";
				$join .= "LEFT JOIN idx_tipe_barang D ON A.id_tipe_barang=D.id ";
				$join .= "LEFT JOIN tbl_master_kode_akun E ON A.kd_akun_persediaan=E.kode_akun ";

			break;

			case "akun_aktifa_tetap":
				$select .= "A.*, B.nama_group_dealer,C.nama_dealer";
				$from .= "tbl_master_akun_aktiva_tetap ";
				$join .= "LEFT JOIN idx_group_dealer B ON A.id_group_dealer=B.id ";
				$join .= "LEFT JOIN tbl_master_dealer C ON A.id_dealer=C.id ";
			break;

			case "akun_saldo":
			break;
			//Master Harga Beli;
			//kategori 1 untuk unit motor
			//kategori 2 untuk toolkit
			//kategori 3 untuk Sparepart
			//kategori 4 untuk Generator
			/*case "harga_belimotor":
			case "harga_beliperalatanservice":
			case "harga_belisparepart":
			case "harga_beligenerator":
				if($param == "harga_belimotor") $kategori = 1;
				if($param == "harga_beliperalatanservice") $kategori = 2;
				if($param == "harga_belisparepart") $kategori = 3;
				if($param == "harga_beligenerator") $kategori = 4;
				
				$select .= "A.*, B.nama_group_barang, C.kategori_barang";
				$from 	.= "tbl_master_hargabeli";
				$join	.= "LEFT JOIN idx_group_barang B ON A.kode_group_barang = B.kode_group_barang ";
				$join	.= "LEFT JOIN idx_kategori_barang C ON A.kode_kategori_barang = C.kode_kategori_barang ";
				$where	.= " AND A.kategori_harga_beli = '".$kategori."' ";
			break;
			
			//Master Harga Jual;
			//kategori 1 untuk unit motor
			//kategori 2 untuk sparepart
			//kategori 3 untuk generator
			case "harga_jualmotor":
			case "harga_jualsparepart":
			case "harga_jualgenerator":
				if($param == "harga_jualmotor") $kategori = 1;
				if($param == "harga_jualsparepart") $kategori = 2;
				if($param == "harga_jualgenerator") $kategori = 3;
				
				$select .= "A.*, B.nama_group_barang, C.kategori_barang, D.nama_dealer";
				$from 	.= "tbl_master_hargajual";
				$join	.= "LEFT JOIN idx_group_barang B ON A.kode_group_barang = B.kode_group_barang ";
				$join	.= "LEFT JOIN idx_kategori_barang C ON A.kode_kategori_barang = C.kode_kategori_barang ";
				$join	.= "LEFT JOIN tbl_master_dealer D ON A.kode_dealer = D.kode_dealer ";
				$where	.= " AND A.kategori_harga_jual = '".$kategori."' ";
			break;
			
			case "harga_jasaservice":
				$select .= "A.*, B.nama_dealer, C.jenis_service";
				$from 	.= "tbl_master_hargajasaservice";
				$join	.= "LEFT JOIN tbl_master_dealer B ON A.kode_dealer = B.kode_dealer ";
				$join	.= "LEFT JOIN tbl_master_service C ON A.kode_jenis_service = C.kode_service ";
			break;*/

			
		}
		$sql = "
				SELECT $select
				FROM $from A
				$join
				$where
		";

		
		return $this->mdealer->get_json($sql); 
	}
	
	function getdatatambahan($type="", $param1="", $param2="", $param3=""){
		switch($type){
			case 'wilayah_dealer':				
				$sql = "
					SELECT C.nama_provinsi, D.nama_kabupaten, E.nama_kecamatan, F.nama_kelurahan, A.kode_pos
					FROM tbl_master_wilayah A
					LEFT JOIN idx_wilayah_provinsi C ON A.id_provinsi = C.id 
					LEFT JOIN idx_wilayah_kabupaten D ON A.id_kabupaten = D.id
					LEFT JOIN idx_wilayah_kecamatan E ON A.id_kecamatan = E.id
					LEFT JOIN idx_wilayah_kelurahan F ON A.id_kelurahan = F.id
					WHERE A.id = '".$param1."'
				";
				$data = $this->db->query($sql)->row();
				if($data) return json_encode($data);
				else return '';
			break;
			case 'harga_jual' :
				$sql = "
					SELECT * FROM view_master_harga_jual WHERE id_barang = '".$param1."'
					ORDER BY tingkat_hrg_jual ASC
				";
				$data = $this->db->query($sql)->result_array();
				if($data) return $data;
				else return '';
			break;
		}
		
	}
	
	function getrecord($type="", $param1="", $param2="", $param3=""){
		$wh = "A.id";
		switch($type){
			// master organisasi
			case 'wilayah':
				$table = "tbl_master_wilayah";
			break;
			case 'dealer':
				$table = "tbl_master_dealer";
			break;
			case 'supplier':
				$table = "tbl_master_supplier";
			break;
			case 'customer':
				$table = "tbl_master_customer";
			break;
			case 'gudang':
				$table = "tbl_master_gudang";
			break;
			case 'karyawan':
				$table = "tbl_master_karyawan";
			break;
			case 'pihak':
				$table = "tbl_master_pihak_ketiga";
			break;	
			case 'kas':
				$table = "tbl_master_kas";
			break;	
			case 'aktiva':
				$table = "tbl_master_aktiva";
			break;

			// master barang dan jasa
			case 'warna':
				$table = "tbl_master_warna";
			break;
			case 'motor':
				$table = "view_master_motor";
			break;
			case 'service':
				$table = "tbl_master_service";
			break;
			case 'sparepart':
			case 'generator':
			case 'peralatan_service':
			case 'barang_promosi':
				$table = "view_master_barang";
			break;
			case 'gambar_barang':
				$wh = "A.kode_gambar";
				$table = 'tbl_master_gambar_barang';
			break;
			case 'harga_jual':
				$wh = "A.id_barang";
				$table = "view_master_harga_jual";
			break;

			// master proses
			case "proses_promosi_unit":
			case "proses_promosi_service":
			case "proses_promosi_sparepart":
				$wh = "A.id";
				$table = "tbl_master_program_promosi";
			break;

			case "promosi_dealer":
				$wh = "A.id_promosi";
				$table = "tbl_master_promosi_dealer";
			break;

			case "promosi_hadiah":
				$wh = "A.id_promosi";
				$table = "tbl_master_promosi_hadiah";
			break;

			case "hadiah_lsg":
				$wh = "A.id_promosi";
				$table = "tbl_master_hadiah_langsung";
			break;

			case "kredit_dealer":
				$wh = "A.id_promosi";
				$table = "tbl_master_promosi_kredit_dealer";
			break;

			case "kredit_leasing":
				$wh = "A.id_promosi";
				$table = "tbl_master_promosi_kredit_leasing";
			break;

			case "paket":
				$wh = "A.id_promosi";
				$table = "tbl_master_penjualan_paket";
			break;

			case "proses_bbn_unit":
			break;

			case "proses_pembiayaan_unit":
				$wh = "A.id";
				$table = "tbl_master_proses_pembiayaan";
			break;

			case "support_subsidi":
			case "support_incentive":
			case "support_promo":
				$wh = "A.id_proses_pembiayaan";
				$table = "tbl_master_jenis_support";
			break;

			case "support_subsidi_add":
				$wh = "A.id_proses_pembiayaan";
				$table = "tbl_master_subsidi_add";
			break;

			case "proses_service_ksg":
			break;


			// master akutansi
			case "kode_akun":
				$table = "tbl_master_kode_akun";
			break;

			case "akun_kas":
				$table = "tbl_master_akun_kas";
			break;

			case "akun_barang_jasa":
				$table = "tbl_master_akun_barang_jasa";
			break;

			case "akun_aktifa_tetap":
				$table = "tbl_master_akun_aktiva_tetap";
			break;

			case "akun_saldo":
			break;

		}
		
		//$type == 'gambar_barang' ? $wh = "A.kode_gambar" : $wh = "A.id";
		
		$sql = "
			SELECT A.*
			FROM $table A
			WHERE $wh = '".$param1."'
		";

		if ($type == 'support_subsidi') {
			$sql = $sql . " AND A.jenis_support = 'S' ";
		} if ($type == 'support_incentive') {
			$sql = $sql . " AND A.jenis_support = 'I' ";
		} if ($type == 'support_promo') {
			$sql = $sql . " AND A.jenis_support = 'P' ";
		}
		
		if ($type == 'gambar_barang' || $type == 'harga_jual' || 
			$type == 'promosi_dealer' || $type == 'promosi_hadiah' ||
			$type == 'kredit_dealer' || $type == "kredit_leasing" || 
			$type == 'hadiah_lsg' || $type == "paket" ||
			$type == 'support_subsidi' || $type == "support_subsidi_add" ||
			$type == 'support_incentive' || $type == "support_promo") {
			$results = $this->db->query($sql)->result_array();
		}else{
			$results = $this->db->query($sql)->row();
		}
		return $results;
	}
	
	function removedata($type, $id) {
		switch($type) {
			case 'warna':
				$table = 'tbl_master_warna';
			break;
			case 'motor':
				$table = 'tbl_master_motor';
			break;
			case 'sparepart':
			case 'generator':
			case 'peralatan_service':
			case 'barang_promosi':
				$table = 'tbl_master_barang';
			break;
			case 'gambar_barang':
				$upload_path = "./repository/master/foto_barang/";
				$sqlPict = $this->db->query("SELECT file_name FROM tbl_master_gambar_barang 
											WHERE kode_gambar = '".$id."' ")->result_array();
				foreach ($sqlPict as $img) {
					if(file_exists($upload_path.$img['file_name'])){
						unlink($upload_path.$img['file_name']);
					}
				}
				$table = 'tbl_master_gambar_barang';
			break;
			case 'hargajual':
				$table = "tbl_master_harga_jual";
			break;
			case 'service':
				$table = "tbl_master_service";
			break;


			// master proses
			case "promodealer":
				$table = "tbl_master_promosi_dealer";
			break;

			case "promohadiah":
				$table = "tbl_master_promosi_hadiah";
			break;

			case "kredit_dealer":
				$table = "tbl_master_promosi_kredit_dealer";
			break;

			case "kredit_leasing":
				$table = "tbl_master_promosi_kredit_leasing";
			break;

			case "hadiah_lsg":
				$table = "tbl_master_hadiah_langsung";
			break;

			case "paket":
				$table = "tbl_master_penjualan_paket";
			break;

			case "proses_promosi_unit":
			case "proses_promosi_service":
			case "proses_promosi_sparepart":
				$table = "tbl_master_program_promosi";
			break;

			case "proses_bbn_unit":
			break;

			case "proses_pembiayaan_unit":
				$table = "tbl_master_proses_pembiayaan";
			break;

			case "support_product":
				$table = "tbl_master_jenis_support";
			break;

			case "support_sub_add":
				$table = "tbl_master_subsidi_add";
			break;

			case "proses_pembiayaan_unit":
				$table = "tbl_master_proses_pembiayaan";
			break;

			case "proses_service_ksg":
			break;

			// master akutansi
			case "kode_akun":
				$table = "tbl_master_kode_akun";
			break;

			case "akun_kas":
				$table = "tbl_master_akun_kas";
			break;

			case "akun_barang_jasa":
				$table = "tbl_master_akun_barang_jasa";
			break;

			case "akun_aktifa_tetap":
				$table = "tbl_master_akun_aktiVa_tetap";
			break;

			case "akun_saldo":
			break;
		}
		
		$type == 'gambar_barang' ? $wh = "kode_gambar" : $wh = "id";
		
		$sql = "DELETE FROM $table
				WHERE $wh = '".$id."' ";
		
		return $this->db->query($sql);
	}
	
	function getcombo($type="", $param1="", $param2="", $param3=""){
		$where = "WHERE 1=1 ";
		$join  = "";
		switch($type){
			case 'idx_wilayah_provinsi':
				$id = 'A.id';
				$value = 'A.nama_provinsi';
			break;
			case 'idx_wilayah_kabupaten':
				$id = 'A.id';
				$value = 'A.nama_kabupaten';			
			break;
			case 'idx_wilayah_kecamatan':
				$id = 'A.id';
				$value = 'A.nama_kecamatan';			
			break;
			case 'idx_wilayah_kelurahan':
				$id = 'A.id';
				$value = 'A.nama_kelurahan';						
			break;
			case 'idx_group_dealer':
				$id = 'A.id';
				$value = 'A.nama_group_dealer';						
			break;
			case 'tbl_master_wilayah':
				$id = 'A.id';
				$value = 'B.nama_kelurahan';
				$join = " LEFT JOIN idx_wilayah_kelurahan B ON A.id_kelurahan = B.id ";
			break;
			case 'idx_group_supplier':
				$id = 'A.id';
				$value = 'A.nama_group_supplier';
			break;
			case 'idx_group_customer':
				$id = 'A.id';
				$value = 'A.nama_group_customer';
			break;
			case 'idx_agama':
				$id = 'A.id';
				$value = 'A.nama_agama';
			break;
			case 'idx_status_tempat_tinggal':
				$id = 'A.id';
				$value = 'A.status_tmp_tinggal';
			break;
			case 'idx_jenis_pekerjaan':
				$id = 'A.id';
				$value = 'A.nama_jenis_pekerjaan';
			break;
			case 'tbl_master_dealer':
				$id = 'A.id';
				$value = 'A.nama_dealer';
				if ($param2 != "") {
					$where = "WHERE A.id_group_dealer = '".$param2."' ";
				}
			break;
			case 'tbl_master_supplier':
				$id = 'A.id';
				$value = 'A.nama_supplier';
				if ($param2 != "") {
					$where = "WHERE A.id_group_supplier = '".$param2."' ";
				}
			break;
			case 'idx_divisi':
				$id = 'A.id';
				$value = 'A.nama_divisi';
			break;
			case 'idx_status_karyawan':
				$id = 'A.id';
				$value = 'A.status_karyawan';
			break;
			case 'idx_group_barang':
				$id = 'A.id';
				$value = 'A.nama_group_barang';
			break;
			case 'idx_kategori_barang':
				$id = 'A.id';
				$value = 'A.kategori_barang';
				if ($param2 != "") {
					$where = "WHERE A.id_group_barang = '".$param2."' ";
				}
			break;
			case 'idx_group_service':
				$id = 'A.id';
				$value = 'A.nama_group_service';
			break;
			case 'idx_kategori_service':
				$id = 'A.id';
				$value = 'A.nama_kategori_service';
				$where = "WHERE A.id_group_service = '".$param2."' ";
			break;
			case 'idx_akun_type':
				$id = 'A.id';
				$value = 'A.akun_type';
				//$where = "WHERE A.id_group_service = '".$param2."' ";
			break;
			case 'tbl_master_kas':
				$id = 'A.id';
				$value = 'A.nama_kas';
				$where = "WHERE A.id_dealer = '".$param2."' ";
			break;
			case 'tbl_master_kode_akun':
				$id = 'A.kode_akun';
				$value = 'A.nama_akun';
				$where = " WHERE A.status = 'Y' ";
			break;
		}
		
		$sql = "
			SELECT ".$id." as code, 
					".$value." as value
			FROM ".$type." A
			$join
			$where
		";
		
		return $this->db->query($sql)->result_array();
	}

}